package carassius.DAL;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.AbstractMap.SimpleEntry;

/**
 *
 * @author siebz0r
 */
public class OptimalAquariumWaterConditionRow
		extends Row<SimpleEntry<Integer, String>>
{
	//<editor-fold defaultstate="collapsed" desc="Fields">
	private final AquariumRow _aquarium;
	private final WaterConditionRow _condition;
	private Double _minValue, _maxValue;
	private final Object MIN_VALUE_LOCK, MAX_VALUE_LOCK;
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Constructors">
	public OptimalAquariumWaterConditionRow(ResultSet resultSet) throws SQLException
	{
		super(new SimpleEntry<Integer, String>(resultSet.getInt("aquarium_id"), resultSet.getString("water_condition")));
		_aquarium = Database.getAquariumTable().getByPrimaryKey(resultSet.getInt("aquarium_id"));
		_condition = Database.getWaterConditionTable().getByPrimaryKey(resultSet.getString("water_condition"));
		_minValue = resultSet.getDouble("min_value");
		if (resultSet.wasNull())
		{
			_minValue = null;
		}
		_maxValue = resultSet.getDouble("max_value");
		if (resultSet.wasNull())
		{
			_maxValue = null;
		}
		MIN_VALUE_LOCK = new Object();
		MAX_VALUE_LOCK = new Object();
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Public Properties">
	public WaterConditionRow getCondition()
	{
		return _condition;
	}

	public Double getMaxValue()
	{
		synchronized (MAX_VALUE_LOCK)
		{
			return _maxValue;
		}
	}

	public boolean setMaxValue(Double maxValue)
	{
		boolean result = false;
		synchronized (MAX_VALUE_LOCK)
		{
			if (maxValue != _maxValue)
			{
				try
				{
					String sql = "UPDATE optimal_aquarium_water_condition SET max_value = ? where aquarium_id = ? and water_condition = ?";
					PreparedStatement st = Database.prepareStatement(sql);
					st.setObject(1, maxValue, Types.DOUBLE);
					st.setInt(2, _aquarium.getId());
					st.setString(3, _condition.getName());

					if (st.executeUpdate() == 1)
					{
						_maxValue = maxValue;
						result = true;
					}
				}
				catch (SQLException ex)
				{
					System.out.println(ex.toString());
				}
			}
		}
		return result;
	}

	public Double getMinValue()
	{
		synchronized (MIN_VALUE_LOCK)
		{
			return _minValue;
		}
	}

	public boolean setMinValue(Double minValue)
	{
		boolean result = false;
		synchronized (MIN_VALUE_LOCK)
		{
			if (minValue != _minValue)
			{
				try
				{
					String sql = "UPDATE optimal_aquarium_water_condition SET min_value = ? where aquarium_id = ? and water_condition = ?";
					PreparedStatement st = Database.prepareStatement(sql);
					st.setObject(1, minValue, Types.DOUBLE);
					st.setInt(2, _aquarium.getId());
					st.setString(3, _condition.getName());

					if (st.executeUpdate() == 1)
					{
						_minValue = minValue;
						result = true;
					}
				}
				catch (SQLException ex)
				{
					System.out.println(ex.toString());
				}
			}
		}
		return result;
	}

	public AquariumRow getAquarium()
	{
		return _aquarium;
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Public Methods">
	@Override
	public boolean equals(Object obj)
	{
		if (obj == null)
		{
			return false;
		}
		if (getClass() != obj.getClass())
		{
			return false;
		}
		final OptimalAquariumWaterConditionRow other = (OptimalAquariumWaterConditionRow) obj;
		if (this._aquarium != other._aquarium && (this._aquarium == null || !this._aquarium.equals(other._aquarium)))
		{
			return false;
		}
		if (this._condition != other._condition && (this._condition == null || !this._condition.equals(other._condition)))
		{
			return false;
		}
		if (Double.doubleToLongBits(this._minValue) != Double.doubleToLongBits(other._minValue))
		{
			return false;
		}
		if (Double.doubleToLongBits(this._maxValue) != Double.doubleToLongBits(other._maxValue))
		{
			return false;
		}
		return true;
	}

	@Override
	public int hashCode()
	{
		int hash = 7;
		return hash;
	}

	@Override
	public String toString()
	{
		return _condition.toString();
	}
	//</editor-fold>
}
